package com.kwd.news.service.impl;

import com.kwd.news.dao.VersionUpgradeDao;
import com.kwd.news.model.VersionUpgrade;
import com.kwd.news.service.VersionUpgradeService;
import com.meirengu.exception.BusinessException;
import com.meirengu.service.impl.BaseServiceImpl;
import com.meirengu.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * VersionUpgrade服务实现层
 *
 * @author 建新
 * @create Mon Mar 27 11:05:06 CST 2017
 */
@Service
public class VersionUpgradeServiceImpl extends BaseServiceImpl<VersionUpgrade> implements VersionUpgradeService {

    private static Logger LOGGER = LoggerFactory.getLogger(VersionUpgradeServiceImpl.class);

    @Autowired
    VersionUpgradeDao versionUpgradeDao;

    @Override
    @Transactional
    public boolean updateVersion(VersionUpgrade vu) {

        try {
            int insertNum = insert(vu);
            if (insertNum == 1) {
                versionUpgradeDao.updateVersion(vu);
                return true;
            } else {
                LOGGER.warn(">> VersionUpgradeServiceImpl.insert fail......");
                return false;
            }
        } catch (Exception e) {
            throw new BusinessException("VersionUpgradeServiceImpl.updateVersion throw exception: {}", e);
        }

    }

    @Override
    public VersionUpgrade getLastVersion(Integer appId, Integer appChannel, Integer status, String appCode) {
        VersionUpgrade vu = new VersionUpgrade();
        vu.setAppId(appId);
        vu.setAppChannel(appChannel);
        vu.setStatus(status);
        if (!StringUtil.isEmpty(appCode)) {
            vu.setAppCode(appCode);
        }
        return versionUpgradeDao.getLastVersion(vu);
    }
}
